package com.sdhs.system.syncdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdhs.common.utils.ConvertUtils;
import com.sdhs.system.syncdata.domain.SyncSysRole;
import com.sdhs.system.syncdata.dto.SysRoleDTO;
import com.sdhs.system.syncdata.mapper.SyncSysRoleMapper;
import com.sdhs.system.syncdata.service.SyncSysRoleService;
import com.sdhs.system.syncdata.util.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 角色管理(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2023-02-27 11:05:38
 */
@Service("sysRoleService")
public class SyncSysRoleServiceImpl extends ServiceImpl<SyncSysRoleMapper, SyncSysRole> implements SyncSysRoleService {
    @Resource
    private SyncSysRoleMapper sysRoleDao;

    @Override
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public Object save(SysRoleDTO sysRoleDTO) {
        String createor=sysRoleDTO.getBimRemoteUser();

        SyncSysRole entity =  ConvertUtils.sourceToTarget(sysRoleDTO, SyncSysRole.class);
        entity.setCreateBy(createor);
        entity.setCreateTime(new Date());
        entity.setUpdateBy(createor);
        entity.setUpdateTime(new Date());
//        Random.
//        String id= TextUtil.getRandom(19);
//        entity.setRoleId(Long.parseLong(id));
        entity.setRoleId(null);
        int insert = this.sysRoleDao.insert(entity);
        String  id = String.valueOf(entity.getRoleId());

        Object result = TransforUtil.getResult(sysRoleDTO.getBimRequestId(), id, insert, "uid", 1);

        return result;
    }

    @Override
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public Object update(SysRoleDTO sysRoleDTO) {
        SyncSysRole entity =  ConvertUtils.sourceToTarget(sysRoleDTO, SyncSysRole.class);
        //QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
//        String password=entity.getPassword();
//        if(TextUtil.isNotNull(password)){
//            String pwd = getPwd(password);
//            entity.setPassword(pwd);
//        }

        entity.setUpdateBy(sysRoleDTO.getBimRemoteUser());
        entity.setUpdateTime(new Date());
        //queryWrapper.eq("id",dto.getBimUid());
        entity.setRoleId(Long.parseLong(sysRoleDTO.getBimRid()));
        QueryWrapper<SyncSysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",entity.getRoleId());
        SyncSysRole sysRole = sysRoleDao.selectOne(queryWrapper);
        Result result=new Result<>();
        result.setBimRequestId(sysRoleDTO.getBimRequestId());
        if(!TextUtil.isNull(sysRole)){
            int i=1;
//            int i = sysRoleDao.updateRole(entity);


            if(i==1){
                return result;
            }else{
                return result.error("修改失败");
            }
        }else{
            return result.error("角色不存在");
        }
    }

    @Override
    public Object delete(String id) {
//        int i = sysRoleDao.deleteRole(id);
        int i=1;
        Result result=new Result();
        if(i==1){
            return result;
        }else{
            return result.error("删除失败");
        }
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SyncSysRole queryById(String id) {
        return this.sysRoleDao.queryById(id);
    }

    @Override
    public Object getAllRoleId(String random) {
        String id=getRootId("路网管理智慧大脑");
        //List<String> roleIds = sysRoleDao.getChildRoles(id);
//        List<String> roleIds = getAllChildRole(id);
        List<String> roles=new ArrayList<>();
        List<String> roleIds = getAllChildRole(id,roles);
//        QueryWrapper<SysRole> queryWrapper1=new QueryWrapper<>();
//        queryWrapper1.eq("pid",id);
//        queryWrapper1.select("id");
////        List<SysRole> sysRoles = sysRoleDao.selectList(queryWrapper1);
////        sysRoles.add(sysRole);
//        List<Object> roleIds = sysRoleDao.selectObjs(queryWrapper1);
//        roleIds.add(id);
        ResultRole resultRole=new ResultRole();
        resultRole.setBimRequestId(random);
        if(roleIds!=null && roleIds.size()!=0){
            resultRole.setRoleIdList(roleIds);
        }else{
            resultRole.setRoleIdList(new ArrayList<>());
        }
        return resultRole;
    }



    @Override
    public Object getRoleById(String random, String uid) {
        QueryWrapper<SyncSysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",uid);
        //"id",
        queryWrapper.select("id","pid","name","status");
        List<Map<String, Object>> sysRole = sysRoleDao.selectMaps(queryWrapper);
        //SysUser sysUser=sysUserMapper.selectOne(queryWrapper);
        ResultUser user=new ResultUser();
        user.setBimRequestId(random);
        Object jsonObject;
        if(sysRole!=null&&sysRole.size()!=0){
            user.setUserIdList(sysRole);
            jsonObject = TransforUtil.getJsonObject(user,"role",1);
        }else{
            user.setUserIdList(new ArrayList<>());
            jsonObject = TransforUtil.getJsonObject(user,"role",3);
        }
        return jsonObject;
    }

    @Override
    public List<String> getAllChildRole(String pid,List<String> roles) {

        List<String> childRoles = sysRoleDao.getChildRoles(pid);
        if(childRoles!=null && !childRoles.isEmpty()){
            for(String child:childRoles){
                List<String> childRoles1 = sysRoleDao.getChildRoles(child);
                if(childRoles1!=null && !childRoles1.isEmpty()){
                    roles.add(child);
                    getAllChildRole(child,roles);
                }else{
//                    allChilds=new ArrayList<>();
                    roles.add(child);
//                    return childRoles;
//                    childRoles.addAll(childRoles1);
                }
            }
            roles.add(pid);
        }

        return roles;
    }

    @Override
    public List<String> getAllChildRoles(List<String> pids) {

        return null;
    }

    @Override
    public String getRootId(String name) {
        QueryWrapper<SyncSysRole> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("status",0);
        queryWrapper.eq("name",name);

        SyncSysRole sysRole = sysRoleDao.selectOne(queryWrapper);
        String id="";
        if(sysRole!=null){
            id= sysRole.getRoleId().toString();
        }
        return id;
    }


}
